package com.eddie.juc.cf;

import com.eddie.juc.IThreadPool;
import lombok.SneakyThrows;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
 * @author Eddie
 * @date 2022/08/17 15:36
 **/
public class CompletableFutureBuildDemo {
    @SneakyThrows
    public static void main(String[] args) {
        //使用自定义线程池
        IThreadPool threadPool = IThreadPool.getInstance();

//        CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() -> {
//            System.out.println(Thread.currentThread().getName());
//            //暂停几秒
//            try {TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {throw new RuntimeException(e);}
//        }, threadPool);
//        System.out.println(completableFuture.get());

        CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName());
            //暂停几秒
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return "hello supplyAsync";
        }, threadPool);
        System.out.println(completableFuture.get());
        threadPool.shutdown();
    }
}
